package com.loto.insurance.mis.web.controller.ajax;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.loto.insurance.mis.web.authority.INSAuthority;
import com.loto.insurance.mis.web.entity.ajax.BaseJson;
import com.loto.insurance.mis.web.entity.ajax.GridJson;
import com.loto.insurance.mis.web.entity.ajax.InsCompany;
import com.loto.insurance.mis.web.enumeration.IsActiveEnum;
import com.loto.insurance.mis.web.enumeration.MenuEnum;
import com.loto.insurance.mis.web.enumeration.ResultTypeEnum;
import com.loto.insurance.mis.web.service.InsCompanyService;
import com.loto.insurance.mis.web.service.ProductInfoService;
import com.loto.insurance.mis.web.entity.Pager;

@Controller
@SessionAttributes("ins_user")
public class InsCompanyJsonController {

  @Autowired
  InsCompanyService insCompanyService;

  @Autowired
  ProductInfoService productInfoService;

  @INSAuthority(authorityMenu = MenuEnum.INSURER_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/insurer/list.json", method = RequestMethod.POST)
  public ModelAndView companyomerlist(
	  @RequestParam(value = "companyName", required = false) String companyName,
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  @RequestParam(value = "rows", required = false, defaultValue = "20") int rows) {
	Map<String, Object> params = new HashMap<String, Object>();

	if (companyName != null) {
	  params.put("companyName", companyName);
	}
	
	Pager pager = new Pager();
	pager.setPageSize(rows);

	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	List<com.loto.insurance.mis.web.entity.InsCompany> companyList = insCompanyService
	    .findAllInsCompany(params);

	List<InsCompany> list = new ArrayList<InsCompany>();
	for (com.loto.insurance.mis.web.entity.InsCompany ins : companyList) {
	  InsCompany i = new InsCompany();
	  i.setCompanyLogo(ins.getCompanyLogo());
	  i.setCompanyName(ins.getCompanyName());
	  i.setCompanyShortName(ins.getCompanyShortName());
	  i.setCompanyWebsite(ins.getCompanyWebsite());
	  i.setCompanyEx(ins.getCompanyEx());
	  i.setCompanyId(ins.getCompanyId());

	  int companyStatus = ins.getCompanyStatus();
	  for (IsActiveEnum e : IsActiveEnum.values()) {
		if (e.getIndex() == companyStatus) {
		  i.setCompanyStatus(e.getName());
		  break;
		}
	  }

	  list.add(i);
	}

	pager.setRowCount(insCompanyService.findAllInsCompanyCount(params));

	ModelAndView mav = new ModelAndView();

	GridJson<InsCompany> gridJson = new GridJson<InsCompany>();
	gridJson.setTotal(pager.getRowCount());
	gridJson.setRows(list);

	mav.addObject("result", gridJson);
	return mav;
  }

  @INSAuthority(authorityMenu = MenuEnum.INSURER_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/insurer/add.json", method = RequestMethod.POST)
  public ModelAndView companyomeradd(
	  @RequestParam(value = "name", required = true) String companyName,
	  @RequestParam(value = "shortname", required = true) String companyShortName,
	  @RequestParam(value = "website", required = true) String companyWebSite,
	  @RequestParam(value = "logo", required = true) String companyLogo,
	  @RequestParam(value = "ex", required = true) String companyEx,
	  @RequestParam(value = "status", required = true) int companyStatus) {

	com.loto.insurance.mis.web.entity.InsCompany insCompany = new com.loto.insurance.mis.web.entity.InsCompany();

	insCompany.setCompanyLogo(companyLogo);
	insCompany.setCompanyName(companyName);
	insCompany.setCompanyShortName(companyShortName);
	insCompany.setCompanyWebsite(companyWebSite);
	insCompany.setCompanyEx(companyEx);
	insCompany.setCompanyStatus(companyStatus);

	ModelAndView mav = new ModelAndView();
	BaseJson base = new BaseJson();
	base.setOk(insCompanyService.addInsCompany(insCompany));
	mav.addObject("result", base);
	return mav;
  }

  @INSAuthority(authorityMenu = MenuEnum.INSURER_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/insurer/modify.json", method = RequestMethod.POST)
  public ModelAndView companyomermodify(
	  @RequestParam(value = "id", required = true) long companyId,
	  @RequestParam(value = "shortname", required = true) String companyShortName,
	  @RequestParam(value = "website", required = true) String companyWebSite,
	  @RequestParam(value = "logo", required = true) String companyLogo,
	  @RequestParam(value = "ex", required = true) String companyEx,
	  @RequestParam(value = "status", required = true) int companyStatus) {

	com.loto.insurance.mis.web.entity.InsCompany insCompany = new com.loto.insurance.mis.web.entity.InsCompany();

	insCompany.setCompanyLogo(companyLogo);
	insCompany.setCompanyId(companyId);
	insCompany.setCompanyShortName(companyShortName);
	insCompany.setCompanyWebsite(companyWebSite);
	insCompany.setCompanyEx(companyEx);
	insCompany.setCompanyStatus(companyStatus);

	ModelAndView mav = new ModelAndView();
	BaseJson base = new BaseJson();
	base.setOk(insCompanyService.updateInsCompany(insCompany));
	mav.addObject("result", base);
	return mav;
  }
}
